package com.yh.correct.shiro;

import java.util.LinkedHashMap;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.yh.correct.model.SysPermission;
import com.yh.correct.service.ISysPermissionService;

/**
 * 过滤链
 * @author Administrator
 *
 */
public class FilterChainDefinitionMapFactoryBean {
	/**
	 * log4j日志
	 */
	private static Logger logger = LogManager.getLogger(FilterChainDefinitionMapFactoryBean.class);
	/**
	 * 权限服务层接口
	 */
	@Autowired
	private ISysPermissionService permissionService;
	
	/**
	 * 过滤链map
	 * anon：没有参数，代表可以匿名使用
	 * authc：没有参数，表示需要认证才能使用
	 * @return
	 */
	public LinkedHashMap<String, String> buildFilterChainDefinitionMap(){
		LinkedHashMap<String, String> map = new LinkedHashMap<>();
		map.put("/login.html", "anon");
		map.put("/toLogin", "anon");
		map.put("/refuse.html", "anon");
		map.put("/error.html", "anon");
		map.put("/images/**", "anon");
		map.put("/js/**", "anon");
		map.put("/css/**", "anon");
		map.put("/easyui/**", "anon");
		map.put("/laydate/**", "anon");
		map.put("/logout", "logout");
/*		map.put("/", "anon");*/
		map.put("/**", "authc");
		List<SysPermission> list=null;
		try {
			list = permissionService.findPermissionAll();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for(SysPermission per:list){
			if(per.getUrl()!=null && !per.getUrl().equals("")){
				if(per.getCode()!=null && !per.getCode().equals("")){
					map.put(per.getUrl(), "authc,perms["+per.getCode()+"]");
				}
			}
		}
		return map;
	}
}
